#!/bin/bash
#
# Run the init-solr-home script and source any '.sh' scripts in
# /docker-entrypoint-initdb.d.
# This script is sourced by some of the solr-* commands, so that
# you can run eg:
#
#   mkdir initdb; echo "echo hi" > initdb/hi.sh
#   docker run -v $PWD/initdb:/docker-entrypoint-initdb.d solr
#
# and have your script execute before Solr starts.
#
# Note: scripts can modify the environment, which will affect
# subsequent scripts and ultimately Solr. That allows you to set
# environment variables from your scripts (though you usually just
# use "docker run -e"). If this is undesirable in your use-case,
# have your scripts execute a sub-shell.

set -e

# init script for handling a custom SOLR_HOME
/opt/docker-solr/scripts/init-solr-home

# execute files in /docker-entrypoint-initdb.d before starting solr
while read f; do
    fpath="/docker-entrypoint-initdb.d/$f"
    case "$f" in
        *.sh)     echo "$0: running $fpath"; . "$fpath" ;;
        *)        echo "$0: ignoring $fpath" ;;
    esac
    echo
done < <(ls /docker-entrypoint-initdb.d/ | sort -n)
